import js from "@eslint/js";
import globals from "globals";
import reactHooks from "eslint-plugin-react-hooks";
import reactRefresh from "eslint-plugin-react-refresh";
import tseslint from "typescript-eslint";
import reactPlugin from "eslint-plugin-react";
import importPlugin from "eslint-plugin-import";
import a11yPlugin from "eslint-plugin-jsx-a11y";

export default tseslint.config(
  { ignores: ["dist", "node_modules"] },
  {
    // 使用基本推荐配置而非严格类型检查配置
    extends: [js.configs.recommended, ...tseslint.configs.recommended],
    files: ["**/*.{ts,tsx}"],
    languageOptions: {
      ecmaVersion: 2022,
      globals: {
        ...globals.browser,
        ...globals.es2021,
      },
      parserOptions: {
        project: null, // 不使用项目配置进行类型检查，更宽松
        // 仅在需要更严格类型检查时取消注释下面两行
        // project: ['./tsconfig.app.json', './tsconfig.node.json'],
        // tsconfigRootDir: import.meta.dirname,
      },
    },
    plugins: {
      react: reactPlugin,
      "react-hooks": reactHooks,
      "react-refresh": reactRefresh,
      import: importPlugin,
      "jsx-a11y": a11yPlugin,
    },
    rules: {
      // React 规则
      ...reactHooks.configs.recommended.rules,
      "react-refresh/only-export-components": [
        "warn",
        { allowConstantExport: true },
      ],
      "react/prop-types": "off",
      "react/react-in-jsx-scope": "off",
      "react/display-name": "off", // 关闭组件显示名称检查
      "react/jsx-key": "warn", // 降级为警告
      "react/no-inline-styles": "off", // 禁用内联样式规则

      // TypeScript 宽松规则
      "@typescript-eslint/explicit-function-return-type": "off",
      "@typescript-eslint/explicit-module-boundary-types": "off", // 允许不明确导出函数类型
      "@typescript-eslint/no-unused-vars": [
        "warn",
        {
          argsIgnorePattern: "^_",
          varsIgnorePattern: "^_",
          ignoreRestSiblings: true,
        },
      ],
      "@typescript-eslint/no-explicit-any": "off", // 允许使用any
      "@typescript-eslint/ban-ts-comment": "off", // 允许使用@ts-ignore等注释
      "@typescript-eslint/no-empty-function": "off", // 允许空函数
      "@typescript-eslint/no-non-null-assertion": "off", // 允许非空断言
      "@typescript-eslint/no-empty-interface": "off", // 允许空接口
      // "@typescript-eslint/ban-types": "warn", // 不推荐的类型仅警告
      "@typescript-eslint/no-inferrable-types": "off", // 允许显式类型
      "@typescript-eslint/no-unnecessary-type-constraint": "off", // 允许不必要的类型约束

      // 代码风格规则
      "no-console": "off", // 允许使用console
      "no-debugger": import.meta.env?.PROD ? "warn" : "off", // Use Vite's env variables
      "prefer-const": "warn", // 仅提示使用const
      "no-unused-expressions": "off", // 允许未使用的表达式

      "no-async-promise-executor": "off", // 允许async函数作为Promise的执行器
      // 导入规则
      "import/no-unresolved": "off", // TypeScript已处理
      "import/order": "off", // 不强制导入顺序
      "import/first": "warn",
      "import/no-duplicates": "warn",

      // 辅助功能规则调整为警告
      "jsx-a11y/click-events-have-key-events": "off",
      "jsx-a11y/no-static-element-interactions": "off",
      "jsx-a11y/alt-text": "warn",
      "jsx-a11y/anchor-is-valid": "warn",
    },
    settings: {
      react: {
        version: "detect",
      },
    },
  },
  // 针对非TypeScript文件的配置
  {
    files: ["**/*.{js,jsx}"],
    extends: [js.configs.recommended],
    rules: {
      // 对JS文件也应用宽松规则
      "no-unused-vars": "warn",
      "no-undef": "warn",
    },
  }
);
